PROJECTENGRAM

MODEL-VIEW-CONTROLLER

Model-View-Controller

Tags: #◇Computer Science #◇CCPROG3 #◇Model-View-Controller #◇Conceptual #◇M7


◇Key Definitions

Model-View-Controller (MVC) is a software architectural pattern that separates an application into three main logical components: Model, View, and Controller.

◇Theoretical Framework

  1. Model: Represents the underlying, logical structure of data in a software application and the high-level class associated with it. It manages data, logic, and rules.
  2. View: Represents the UI of the application. It displays the data (the model) to the user and sends user commands to the controller.
  3. Controller: Handles the user request. Typically, the user interacts with the View, which in turn raises appropriate URL requests, this request will be handled by a controller. The controller renders the appropriate view with the model data as a response.

◇Important Formulas

The MVC Flow:
User Action -> View -> Controller -> Updates -> Model -> Notifies -> View -> Updated UI

◇Theoretical Framework: Architectural Diagrams

1. Component Interaction Sequence

2. MVC Class Dependency Structure

◇Code Implementation: Production Java Swing MVC Suite

1. CounterModel.java (Business Logic & Data State)

『』
JAVA
package mvc;

public class CounterModel {
    private int count;

    public CounterModel() {
        this.count = 0;
    }

    public int getCount() {
        return this.count;
    }

    public void increment() {
        this.count++;
    }

    public void decrement() {
        if (this.count > 0) { // Business rule: non-negative counter
            this.count--;
        }
    }

    public void reset() {
        this.count = 0;
    }
}

2. CounterView.java (Graphical Interface & User Event Hooks)

『』
JAVA
package mvc;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

public class CounterView extends JFrame {
    private final JLabel countLabel = new JLabel("0", SwingConstants.CENTER);
    private final JButton incrementBtn = new JButton("+");
    private final JButton decrementBtn = new JButton("-");

    public CounterView() {
        super("MVC Counter Application");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(300, 200);
        this.setLayout(new BorderLayout(10, 10));

        countLabel.setFont(new Font("Monospaced", Font.BOLD, 48));
        this.add(countLabel, BorderLayout.CENTER);

        JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 10, 0));
        buttonPanel.add(decrementBtn);
        buttonPanel.add(incrementBtn);
        this.add(buttonPanel, BorderLayout.SOUTH);

        this.setLocationRelativeTo(null);
    }

    public void setCountDisplay(int value) {
        this.countLabel.setText(String.valueOf(value));
    }

    // Decoupled listener registration (View knows nothing about controller logic)
    public void addIncrementListener(ActionListener listener) {
        this.incrementBtn.addActionListener(listener);
    }

    public void addDecrementListener(ActionListener listener) {
        this.decrementBtn.addActionListener(listener);
    }
}

3. CounterController.java (Mediator & Event Coordinator)

『』
JAVA
package mvc;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CounterController {
    private final CounterModel model;
    private final CounterView view;

    public CounterController(CounterModel model, CounterView view) {
        this.model = model;
        this.view = view;
        this.initController();
    }

    private void initController() {
        // Register ActionListeners on the View
        this.view.addIncrementListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                model.increment();
                view.setCountDisplay(model.getCount());
            }
        });

        this.view.addDecrementListener(e -> {
            model.decrement();
            view.setCountDisplay(model.getCount());
        });
    }
}

4. MainApp.java (Application Entry Point)

『』
JAVA
package mvc;

import javax.swing.SwingUtilities;

public class MainApp {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            CounterModel model = new CounterModel();
            CounterView view = new CounterView();
            new CounterController(model, view);
            view.setVisible(true);
        });
    }
}

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

What is the Model-View-Controller (MVC) pattern?

Q2

In the MVC architecture, what is the primary responsibility of the Model?

Q3

Which component directly receives user commands and raises appropriate URL requests?

Q4

In a Simple Counter App using MVC, what does the Controller do when the '+' button is clicked?

Q5

What is the typical flow of data in the MVC pattern after a user action?

Model-View-Controller | PROJECT ENGRAM